package com.htd.htdapmservice.mybatis;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.htd.htdapmcommon.mybatis.DalBaseMyBatis;
import com.htd.htdapmdmo.AlarmsInfo;
import com.htd.htdapmdmo.OneAlertAlarmsRecordInfo;
import com.htd.htdapmservice.dao.AlarmsInfoDao;


@Repository
public class AlarmsInfoMapper extends DalBaseMyBatis<AlarmsInfo> implements AlarmsInfoDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
    @Override
    public AlarmsInfo getInfoById(int id) {
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("id",id);
        return this.sqlSessionTemplate.selectOne(this.getEntityName() + ".getInfoById", map);
    }
    @Override
	public List<AlarmsInfo> getInfoByAlarmsId(String alarmsId) {
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("alarmsId",alarmsId);
        return this.sqlSessionTemplate.selectList(this.getEntityName() + ".getInfoByAlarmsId", map);
    }

    /**
	 * 保存调用创建告警API时输入的参数到数据库
	 * 
	 */
	@Override
	public int saveAlarmInto(AlarmsInfo alarmsInfo) {
		return this.sqlSessionTemplate.insert(this.getEntityName() + ".insertSelective", alarmsInfo);
	}

    @Override
    public int batchSaveAlarmInto(List<AlarmsInfo> alarmsInfos) {
        return this.sqlSessionTemplate.insert(this.getEntityName() + ".batchInsertSelective", alarmsInfos);
    }

    @Override
    public List<AlarmsInfo> getAlarmsInfoListBySendStatus(String sendStatus) {
        return this.sqlSessionTemplate.selectList(this.getEntityName() + ".findAlarmsInfoListByStatus",sendStatus);
    }

    @Override
    public int updateSendStatusByAlarmsId(Map<String, Object> paramMap) {
        return this.sqlSessionTemplate.update(this.getEntityName() + ".updateSendStatus",paramMap);
    }

    @Override
    public int updateAlarmsStatusById(Map<String, Object> paramMap) {
        return this.sqlSessionTemplate.update(this.getEntityName() + ".updateAlarmsStatus",paramMap);
    }

	/**
	 * 同步表“one_alert_alarms_record”中status的状态到表“alarms_info”
	 * @param Status、alarmId
	 * @return
	 */
	@Override
	public void updateAlarmInfoStatus(final List<OneAlertAlarmsRecordInfo> oneAlertAlarmsRecordInfoList) {
        final String sql = "UPDATE alarms_info SET alarms_status = ?,update_time = NOW() WHERE alarms_id=?";
        this.jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
            @Override
            public int getBatchSize() {
                return oneAlertAlarmsRecordInfoList.size();
            }
            @Override
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                ps.setString(1,oneAlertAlarmsRecordInfoList.get(i).getStatus());
                ps.setString(2,oneAlertAlarmsRecordInfoList.get(i).getEntityId());
            }
        });
	}
	
	   @Override
	    public List<AlarmsInfo> getAlarmsInfoListByAlarmStatus() {
	        return this.sqlSessionTemplate.selectList(this.getEntityName() + ".findAlarmsInfoListByAlarmStatus");
	    }
	   
		/**
		 * 批量保存调用查询告警时输出的参数到数据库
		 * 
		 */
		@Override
		public void batchUpdate(final List<AlarmsInfo> alarmsInfos) {
	        final String sql = "UPDATE alarms_info SET alarms_status = ?,UPDATE_TIME = NOW() WHERE id=?";
	        this.jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
	            @Override
	            public int getBatchSize() {
	                return alarmsInfos.size();
	            }
	            @Override
	            public void setValues(PreparedStatement ps, int i)throws SQLException {
	                ps.setString(1,alarmsInfos.get(i).getAlarmsStatus().getCode());
	                ps.setLong(2,alarmsInfos.get(i).getId());
	            }
	        });
		}

}
